local sk__jizhao = fk.CreateSkill {

  name = "sk__jizhao",

  tags = {},

}



sk__jizhao:addEffect(fk.EventPhaseStart, {
  name = "sk__jizhao",
  anim_type = "support",
  events = { fk.EventPhaseStart, fk.DamageCaused, fk.TurnStart },
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jizhao.name) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Play and not player:isKongcheng() and
            #player.room.alive_players > 1
      elseif event == fk.DamageCaused then
        return target and target:getMark("@sk__jizhao") > 0
      elseif event == fk.TurnStart then
        return target == player and table.find(player.room.alive_players, function(p)
          return p:getMark("@sk__jizhao") > 0
        end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local result = room:askForYiji(player, player:getCardIds("h"), room:getOtherPlayers(player), sk__jizhao.name, 0, 999,
      "#sk__jizhao-give", nil, true)
    for _, ids in pairs(result) do
      if #ids > 0 then
        event:setCostData(self, result)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local result = event:getCostData(self)
    room:doYiji(result, player, sk__jizhao.name)
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if #result[tostring(p.id)] > 0 and not p.dead then
        room:addPlayerMark(p, "@sk__jizhao", 1)
      end
    end
  end,
})
sk__jizhao:addEffect(fk.DamageCaused, {
  name = "sk__jizhao",
  anim_type = "support",
  events = { fk.EventPhaseStart, fk.DamageCaused, fk.TurnStart },
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jizhao.name) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Play and not player:isKongcheng() and
            #player.room.alive_players > 1
      elseif event == fk.DamageCaused then
        return target and target:getMark("@sk__jizhao") > 0
      elseif event == fk.TurnStart then
        return target == player and table.find(player.room.alive_players, function(p)
          return p:getMark("@sk__jizhao") > 0
        end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local result = room:askForYiji(player, player:getCardIds("h"), room:getOtherPlayers(player), sk__jizhao.name, 0,
        999,
        "#sk__jizhao-give", nil, true)
      for _, ids in pairs(result) do
        if #ids > 0 then
          event:setCostData(self, result)
          return true
        end
      end
    elseif event == fk.DamageCaused then
      return room:askForSkillInvoke(player, sk__jizhao.name, nil, "#sk__jizhao1-invoke:" .. target.id .. ":" ..
        data.to.id)
    elseif event == fk.TurnStart then
      return room:askForSkillInvoke(player, sk__jizhao.name, nil, "#sk__jizhao2-invoke")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local result = event:getCostData(self)
      room:doYiji(result, player, sk__jizhao.name)
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if #result[tostring(p.id)] > 0 and not p.dead then
          room:addPlayerMark(p, "@sk__jizhao", 1)
        end
      end
    elseif event == fk.DamageCaused then
      room:removePlayerMark(target, "@sk__jizhao", 1)
      data.damage = data.damage + 1
    elseif event == fk.TurnStart then
      for _, p in ipairs(room:getAlivePlayers()) do
        local n = p:getMark("@sk__jizhao")
        if not p.dead and n > 0 then
          room:setPlayerMark(p, "@sk__jizhao", 0)
          room:loseHp(p, n, sk__jizhao.name)
        end
      end
    end
  end,
})
sk__jizhao:addEffect(fk.TurnStart, {
  name = "sk__jizhao",
  anim_type = "support",
  events = { fk.EventPhaseStart, fk.DamageCaused, fk.TurnStart },
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jizhao.name) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Play and not player:isKongcheng() and
            #player.room.alive_players > 1
      elseif event == fk.DamageCaused then
        return target and target:getMark("@sk__jizhao") > 0
      elseif event == fk.TurnStart then
        return target == player and table.find(player.room.alive_players, function(p)
          return p:getMark("@sk__jizhao") > 0
        end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local result = room:askForYiji(player, player:getCardIds("h"), room:getOtherPlayers(player), sk__jizhao.name, 0,
        999,
        "#sk__jizhao-give", nil, true)
      for _, ids in pairs(result) do
        if #ids > 0 then
          event:setCostData(self, result)
          return true
        end
      end
    elseif event == fk.DamageCaused then
      return room:askForSkillInvoke(player, sk__jizhao.name, nil, "#sk__jizhao1-invoke:" .. target.id .. ":" ..
        data.to.id)
    elseif event == fk.TurnStart then
      return room:askForSkillInvoke(player, sk__jizhao.name, nil, "#sk__jizhao2-invoke")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      local n = p:getMark("@sk__jizhao")
      if not p.dead and n > 0 then
        room:setPlayerMark(p, "@sk__jizhao", 0)
        room:loseHp(p, n, sk__jizhao.name)
      end
    end
  end,
})
sk__jizhao:addEffect(fk.EventLoseSkill, {
  refresh_events = { fk.EventLoseSkill, fk.Deathed },
  can_refresh = function(self, event, target, player, data)
    if target == player and not table.find(player.room:getOtherPlayers(player), function(p)
          return p:hasSkill("sk__jizhao", true, true)
        end) then
      if event == fk.EventLoseSkill then
        return data.skill.name == sk__jizhao.name
      else
        return player:hasSkill("sk__jizhao", true, true)
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      room:setPlayerMark(p, "@sk__jizhao", 0)
    end
  end,
})
sk__jizhao:addEffect(fk.Deathed, {
  refresh_events = { fk.EventLoseSkill, fk.Deathed },
  can_refresh = function(self, event, target, player, data)
    if target == player and not table.find(player.room:getOtherPlayers(player), function(p)
          return p:hasSkill("sk__jizhao", true, true)
        end) then
      if event == fk.EventLoseSkill then
        return data == self
      else
        return player:hasSkill("sk__jizhao", true, true)
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      room:setPlayerMark(p, "@sk__jizhao", 0)
    end
  end,
})

return sk__jizhao
